package com.xo.xodemo.service.impl;


import com.xo.xodemo.common.config.QrConfig;
import com.xo.xodemo.common.config.QueryConfig;
import com.xo.xodemo.model.OrderResult;
import com.xo.xodemo.service.JHService;
import com.xo.xodemo.util.*;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Service
public class JHServiceImpl implements JHService {
    @Override
    public String createQrCode(String userId, String payment, String orderId,String buyType) throws IOException {

        return create(orderId, payment, userId,buyType);
    }

    String create(String orderId, String payment, String userId,String buyType) {
        String TIMEOUT = TomorrowUtil.halfHourStr();
        StringBuffer tmp = new StringBuffer(); //验签字段
        tmp.append("MERCHANTID=");
        tmp.append(QrConfig.MERCHANTID);
        tmp.append("&POSID=");
        tmp.append(QrConfig.POSID);
        tmp.append("&BRANCHID=");
        tmp.append(QrConfig.BRANCHID);
        tmp.append("&ORDERID=");
        tmp.append(orderId);
        tmp.append("&PAYMENT=");
        tmp.append(payment);
        tmp.append("&CURCODE=");
        tmp.append(QrConfig.CURCODE);
        tmp.append("&TXCODE=");
        tmp.append(QrConfig.TXCODE);
        tmp.append("&REMARK1=");
        tmp.append(userId);
        tmp.append("&REMARK2=");
        tmp.append(buyType);
        tmp.append("&RETURNTYPE=");
        tmp.append(QrConfig.RETURNTYPE);
        tmp.append("&TIMEOUT=");
        tmp.append(TIMEOUT);
        tmp.append("&PUB=");
        //默认用32位的
        tmp.append(QrConfig.PUB32TR2);
        Map map = new HashMap();
        map.put("CCB_IBSVersion", "V6");    //必输项
        map.put("MERCHANTID", QrConfig.MERCHANTID);
        map.put("BRANCHID", QrConfig.BRANCHID);
        map.put("POSID", QrConfig.POSID);
        map.put("ORDERID", orderId);
        map.put("PAYMENT", payment);
        map.put("CURCODE", QrConfig.CURCODE);
        map.put("TXCODE", QrConfig.TXCODE);
        map.put("REMARK1", userId);
        map.put("REMARK2", buyType);
        map.put("RETURNTYPE", QrConfig.RETURNTYPE);
        map.put("TIMEOUT", TIMEOUT);
        map.put("MAC", MD5.md5Str(tmp.toString()));
        //页面的html 字符串
        String post = HttpClientUtil.httpPost(QrConfig.bankURL, map);
        return post;
    }
    @Override
    public OrderResult searchIsPayFororderId(String orderId, String page) throws IOException {
        String param = "MERCHANTID=" + QueryConfig.MERCHANTID + "&BRANCHID=" + QueryConfig.BRANCHID + "&POSID=" + QueryConfig.POSID + "&ORDERDATE="
               +"&BEGORDERTIME=" + QueryConfig.BEGORDERTIME + "&ENDORDERTIME=" + QueryConfig.ENDORDERTIME + "&ORDERID="+orderId
                + "&QUPWD=" + "&TXCODE=" + QueryConfig.TXCODE + "&TYPE=" + QueryConfig.TYPE + "&KIND=" + QueryConfig.KIND + "&STATUS=" + QueryConfig.STATUS +
                "&SEL_TYPE=" + QueryConfig.SEL_TYPE + "&PAGE=" + page + "&OPERATOR=" + QueryConfig.OPERATOR + "&CHANNEL=" + QueryConfig.CHANNEL;

        Map map = new HashMap();
        map.put("MERCHANTID", QueryConfig.MERCHANTID);

        map.put("BRANCHID", QueryConfig.BRANCHID);
        map.put("POSID", QueryConfig.POSID);

        map.put("ORDERDATE", "");

        map.put("BEGORDERTIME", QueryConfig.BEGORDERTIME);

        map.put("ENDORDERTIME", QueryConfig.ENDORDERTIME);

        map.put("BEGORDERID", QueryConfig.BEGORDERID);

        map.put("ENDORDERID", QueryConfig.ENDORDERID);

        map.put("QUPWD", QueryConfig.QUPWD);

        map.put("TXCODE", QueryConfig.TXCODE);

        map.put("TYPE", QueryConfig.TYPE);
        map.put("KIND", QueryConfig.KIND);
        map.put("STATUS", QueryConfig.STATUS);
        map.put("ORDERID", orderId);
        map.put("PAGE", page);
        map.put("CHANNEL", QueryConfig.CHANNEL);
        map.put("SEL_TYPE", QueryConfig.SEL_TYPE);
        map.put("OPERATOR", QueryConfig.OPERATOR);
        map.put("MAC", MD5.md5Str(param));
        String ret = HttpClientUtil.httpPost(QueryConfig.bankURLSearch, map);
        OrderResult orderResult = new OrderResult();
        try {
            orderResult = analysis(ret.trim());
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return orderResult;
    }

    @Override
    public String searchIsPayFororderDate(String orderDate, String page) throws IOException {
        String param = "MERCHANTID=" + QueryConfig.MERCHANTID + "&BRANCHID=" + QueryConfig.BRANCHID + "&POSID=" + QueryConfig.POSID + "&ORDERDATE="
                + orderDate + "&BEGORDERTIME=" + QueryConfig.BEGORDERTIME + "&ENDORDERTIME=" + QueryConfig.ENDORDERTIME + "&ORDERID="
                + "&QUPWD=" + "&TXCODE=" + QueryConfig.TXCODE + "&TYPE=" + QueryConfig.TYPE + "&KIND=" + QueryConfig.KIND + "&STATUS=" + QueryConfig.STATUS +
                "&SEL_TYPE=" + QueryConfig.SEL_TYPE + "&PAGE=" + page + "&OPERATOR=" + QueryConfig.OPERATOR + "&CHANNEL=" + QueryConfig.CHANNEL;

        Map map = new HashMap();
        map.put("MERCHANTID", QueryConfig.MERCHANTID);

        map.put("BRANCHID", QueryConfig.BRANCHID);
        map.put("POSID", QueryConfig.POSID);

        map.put("ORDERDATE", orderDate);

        map.put("BEGORDERTIME", QueryConfig.BEGORDERTIME);

        map.put("ENDORDERTIME", QueryConfig.ENDORDERTIME);

        map.put("BEGORDERID", QueryConfig.BEGORDERID);

        map.put("ENDORDERID", QueryConfig.ENDORDERID);

        map.put("QUPWD", QueryConfig.QUPWD);

        map.put("TXCODE", QueryConfig.TXCODE);

        map.put("TYPE", QueryConfig.TYPE);
        map.put("KIND", QueryConfig.KIND);
        map.put("STATUS", QueryConfig.STATUS);
        map.put("ORDERID", "");
        map.put("PAGE", page);
        map.put("CHANNEL", QueryConfig.CHANNEL);
        map.put("SEL_TYPE", QueryConfig.SEL_TYPE);
        map.put("OPERATOR", QueryConfig.OPERATOR);
        map.put("MAC", MD5.md5Str(param));
        String ret = HttpClientUtil.httpPost(QueryConfig.bankURLSearch, map);
        return ret;
    }


    OrderResult analysis(String result) throws SAXException, ParserConfigurationException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        OrderResult o = new OrderResult();
        //创建解析器对象
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document document = null;
        //设置解析的xml文件
        InputStream inputStream = new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8));
        document = builder.parse(inputStream);
        String RETURN_CODE = document.getElementsByTagName("RETURN_CODE").item(0).getTextContent();
        //返回成功
        if (RETURN_CODE.equals("000000")) {
            o.setRETURN_CODE(RETURN_CODE);
            Node queryorderNode = document.getElementsByTagName("QUERYORDER").item(0);
            NodeList nodeList = queryorderNode.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                if (nodeList.item(i).getNodeName().equals("MERCHANTID")) {
                    o.setMERCHANTID(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("BRANCHID")) {
                    o.setBRANCHID(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("POSID")) {
                    o.setPOSID(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("ORDERID")) {
                    o.setORDERID(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("ORDERDATE")) {
                    o.setORDERDATE(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("ACCDATE")) {
                    o.setACCDATE(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("AMOUNT")) {
                    o.setAMOUNT(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("STATUS")) {
                    o.setSTATUS(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("STATUSCODE")) {
                    o.setSTATUSCODE(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("REFUND")) {
                    o.setREFUND(nodeList.item(i).getTextContent());
                }
                if (nodeList.item(i).getNodeName().equals("SIGN")) {
                    o.setSIGN(nodeList.item(i).getTextContent());
                }
            }
        }
        return o;
    }
}
